﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities;
using MyWorkBlogDAL;
using MyWorkBlogMVC.Models;
using PagedList;
using PagedList.Mvc;
namespace MyWorkBlogMVC.Controllers
{
    public class HomeController : Controller
    {
        List<BlogPost> _post = new List<BlogPost>();
        public ActionResult Index(int? page)
        {
            var blogs = Blog.GetBlogsWithPost();
            var blogmodel = new BlogModel()
            {
                Blogs = blogs.ToPagedList(page ?? 1, 5)
            };
            return View(blogmodel);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult Post(int Id, int? page)
        {
            var blogInfo = new Blog();
            _post = BlogPost.GetBlogPostlist(Id);

            blogInfo = Blog.GetBlogWithBlogId(Id);

               
                var postmodel = new PostModel()
                {
                    BlogPosts = _post.ToPagedList(page ?? 1, 3),
                     BlogName = blogInfo.Name,
                Description = blogInfo.Description,
             Username = blogInfo.Username,
              Country = blogInfo.Country,
              FirstName = blogInfo.FirstName,
              LastName = blogInfo.LastName,
              DateOfBirth = blogInfo.DateOfBirth
                };

               
            return View(postmodel);
        }


        public ActionResult PostWithContent(int Id, int? page)
        {

          var  blogPost = BlogPost.GetBlogPostPostWithPostId(Id);
            if (blogPost == null)

                return HttpNotFound();

            var comments = Comment.GetComments(blogPost.BlogPostId);


            var postmodel = new PostWithContentModel()
            {
                BlogPosts = blogPost,
                PostComments = comments.ToPagedList(page ?? 1, 5)
            };


            return View(postmodel);
        }
       
        [HttpPost]
        public ActionResult PostWithContent(PostWithContentModel model, int id)
        {
            if (ModelState.IsValid)
            {
                var postComment = new Comment()
                {
                    Email = model.Email,
                    NameOfCommetor = model.NameOfCommetor,
                    CommentContent = model.CommentContent,
                    CommentDate = DateTime.Now.Date,
                    BlogPostId = BlogPost.GetBlogPostPostWithPostId(id).BlogPostId

                };
                if (Comment.InsertComment(postComment) > 0)
                {
                    return RedirectToAction("PostWithContent", "Home");
                }
            }
            return View(model);
            //return PartialView("_Comment");
        }

        
        
    }
}